/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package horizon.component;

import horizon.graphics.RenderingContext;
import horizon.util.Vector2;
import java.awt.Color;

/**
 *
 * @author J_Laptop
 */
public class AnimationRender extends RenderComponent{

   
    int currentFrame = 0;
    int currentTime = 0;
    //boolean repeats;
    Animation anim;
    
    
    public AnimationRender(String id){
        super(id);
        
        
        
    }
    
    public void setAnimation(Animation a){
        
        if(anim == null){
            
            anim = a;
            currentFrame = 0;
            currentTime = 0;
            
        }
        else if(!anim.equals(a)){
            anim = a;
            currentFrame = 0;
            currentTime = 0;
        }
    }
    
    @Override
    public void render(RenderingContext r) {
        
        r.drawImage(anim.imgs[currentFrame], r.WorldToScreenCoordinates(owner.getPosition()), owner.getRotation(), owner.getScale());
        //r.drawText(imgs[currentFrame], new Vector2(100 , 100), Color.red);
    }

    @Override
    public void update(long elapsedTime) {
        
        currentTime += elapsedTime;
        
        if(currentTime > anim.times[currentFrame]){
            
            if(!(currentFrame == anim.imgs.length - 1)){
                currentFrame++;
            }
            else if(anim.repeats){
                
                currentFrame = 0;
                
            }
            currentTime = 0;
            
        }
        
    }
    
    
}
